This chapter will cover the various properties, methods, and events of the Data control. There will be examples provided throughout in order to illustrate the capabilities of the control.
The Data control is a component of Visual Basic that allows your application to interact with databases of different formats. Using the Data control, you can open a database and define the contents of the underlying Recordset.
A Recordset is a collection of records that are extracted from the database and then manipulated by the Data control. There are three types of Recordsets: dynaset, snapshot, and table. Each of these Recordsets has a variety of properties and methods
that you can apply to it. This chapter and Chapter 28, "The Access Jet Engine," discuss the properties and methods of the Data control and the underlying Recordset.
Through the Data control's RecordSource and RecordSourceType properties, you define the Recordset that your application will work with. Using the Data control, you can view, edit, and update records in the underlying Recordset. You cannot use the Data
control to delete records or create a new database.
The Data control supports the following database formats:
MS Access is the most flexible format of database to use with Visual Basic because MS Access and Visual Basic share the same database engine (called Jet).
Using a combination of bound controls and the Data control, you can create an application that will allow you to move through the records in a Recordset. You can edit these records or add new records to the Recordset with a minimum of programming
effort.
Bound controls provide direct access to the contents of a field in a database. The fields that are available to the bound control are defined by The Recordset contained in the Data control. The DataSource and DataField properties are used in order to
define the contents of a bound control.
Bound controls support all of the Standard Visual Basic text editing and formatting properties and methods. Additionally, bound controls support the DataSource, DataField, and DataChanged properties. The DataSource property is a read/write property at
both run time and design time that refers to the Data control that is resident on the same form as the bound control. The DataField property is a read/write property at both run time and design time that refers to a field contained in the Recordset held by
the Data control. The DataChanged property is a read-only, run-time property that is used in order to determine whether a change has been made to the underlying Recordset through a bound control. Any changes you make to the contents of the bound controls
will be automatically updated to the Recordset whenever the record pointer of the Data control is altered either through the use of the Data control's record movement buttons or through actions performed in your application.
The Data control provides navigation buttons (shown in Figure 27.1) for you to use to move through records.
Figure 27.1. The Data control navigation buttons.
The following example shows how to use the Data control and bound text boxes to display and edit values stored in the TITLES table of the BIBLIO.MDB database that is supplied with Visual Basic. Follow these steps:
Control |
Name |
Caption |
Top |
Left |
Width |
Height |
Form |
form1 |
Bound Form |
1515 |
1920 |
6810 |
5190 |
Data control |
data1 |
Data control 1 |
3360 |
0 |
6495 |
300 |
Label |
lblpubid |
Pub ID. |
180 |
180 |
1215 |
285 |
Label |
lblyear |
Year Pub. |
180 |
3660 |
1215 |
285 |
Label |
lblIsbn |
ISBN |
600 |
180 |
1215 |
285 |
Label |
lblTitle |
Title |
1020 |
180 |
1215 |
285 |
Label |
lblSubject |
Subject |
1440 |
180 |
1215 |
285 |
Label |
lblDescription |
Description |
1860 |
180 |
1215 |
285 |
Label |
lblComments |
Comments |
2280 |
180 |
1215 |
285 |
Label |
lblNotes |
Notes |
2700 |
180 |
1215 |
285 |
Textbox |
txtPubid |
|
180 |
1560 |
855 |
285 |
Textbox |
txtYear |
|
180 |
5040 |
855 |
285 |
Textbox |
txtIsbn |
|
600 |
1560 |
1635 |
285 |
Textbox |
txtTitle |
|
1020 |
1560 |
4635 |
285 |
Textbox |
txtSubject |
|
1440 |
1560 |
4635 |
285 |
Textbox |
txtDescription |
|
1860 |
1560 |
4635 |
285 |
Textbox |
txtComments |
|
2280 |
1560 |
4635 |
285 |
Textbox |
txtNotes |
|
2700 |
1560 |
4635 |
285 |
Command |
cmdExit |
E&xit |
4080 |
5400 |
795 |
295 |
Figure 27.2. The bound fields illustrates the Data control.
You can use the Data control's record navigation buttons to move through all of the records contained in the titles Recordset. Additionally, you can make changes to the contents of the Recordset by altering the information contained in the text boxes
and using the Data control to change the current record.
The preceding example is a very simple examination of the capabilities of the Data control. The rest of this chapter builds on this example in order to fully explore the different properties, methods, and events available to the Data control.
The Data control supports all of the standard properties, such as control placement, that are associated with Visual Basic controls. It also supports properties that relate to data manipulation. You can manipulate these data manipulation properties for
read/write access at both design and run time. The following sections describe each of the data manipulation properties.
You use the Align property to determine the location of the Data control in relation to the form at run time as well as design time. You can align the Data control with the top, left, bottom, or right edge of the form. You also can choose to have no
alignment and place the control anywhere on the form. The default value of the Align property is no alignment.
Add the following line to the Form_Load event or make the change using the properties editor and run your project:
Data1.align= 2
The Data control should now be aligned along the button of the form under the Exit button.
The BOFAction property determines what action the Data control takes when the BOF (beginning of file) property of the Recordset is true. MoveFirst and BOF are the two possible settings for the BOFAction property.
The MoveFirst setting causes the Data control to reposition the Recordset to the first record in the set and make it the current record when the BOF condition is true. To set the BOFAction property to this setting at run time, use the following
expression (data1 represents the name of the Data control):
data1.BOFaction= vbBOFActionMoveFirst
If the BOFAction property is set to BOF, the Data control takes no action when the BOF condition is true. At this point, there is no current record and any attempt to edit the displayed record results in a No Current Record error message. If the
Data control is visible, then the move to previous navigation button is disabled. To set the BOFAction property to this setting at run time, use the following expression (data1 represents the name of the Data control):
data1.BOFaction= vbBOFActionBOF
To demonstrate the behavior of the BOFAction property, perform the following steps.
Figure 27.3. This is how a screen looks when you are at the beginning of a file.
You use the Connect property when you are accessing databases from a program other than MS Access. The Connect property specifies the type of database that you are connecting to. Note that only files of the specified type are displayed in the Database
Selection dialog box. If you are connecting to an ODBC database, for example, you need to specify ODBC in the Connect property and perhaps supply additional information such as USERID and PASSWORD. The additional information depends on the type of ODBC
database you're using. (The ODBC database driver supplies further information on the connect string for ODBC databases.)
The following is an example of an expression that sets the Connect property at run time:
data1.connect= " dBASE III;"
The contents of the DataBaseName property depend on what type of database you're connecting to. If you are connecting to an MS Access database, the property contains the name of the database that contains the tables you want to access. If you are
connecting to a Btrieve database, the property contains the name of the Btrieve Data Definition file relating to the tables you want to access. If you are connecting to an ODBC database, the property is blank because the name of the data source is
determined by settings in the Connect property . For all other supported databases, the DataBaseName property is set to the directory that contains the files that contain the data you want to access. To select the database file or the directory from a file
selection dialog box, select the (...) in the properties editor.
Follow these steps to learn how the DataBaseName property works:
The EOFAction property determines what action the Data control takes when the EOF (end of file) property is true. MoveLast, EOF, and AddNew are the possible settings for this property.
The MoveLast setting causes the Data control to reposition the Recordset to the last record and make that record the current record when the Data control encounters an EOF condition of true. The following expression demonstrates how to specify this
setting at run time (data1 is the name of the Data control):
data1.EOFaction= vbEOFActionMoveLast
The EOF setting causes the Data control to take no action when the EOF condition is true. At this point, there is no current record and any attempt to edit the displayed record results in a No Current Record error message. If the Data control is
visible, the move to the next record button is disabled. To specify this setting at run time, use the following expression:
data1.EOFAction= vbEOFActionEOF
The AddNew setting causes the Data control to add a new record to the table and make that record the current record when the EOF condition is true. To specify this setting at run time, use the following expression:
data1.EOFaction= vbEOFActionAddNew
Follow these steps to use the AddNew setting of the EOFAction property:
The Exclusive property determines whether your application is opening the database for exclusive use, which means no other users can access the database. You can set this property to true (no other users can access the database) or false (other users
are able to access the database). The following expression shows how you can set this property at run time:
dim filepath as string filepath="c:\vb40\" data1.databasename= filepath&"biblio.mdb" data1.exclusive= true data1.refresh
To practice using the Exclusive property, perform the following steps:
The Options property specifies the characteristics of the Recordset that is created by the Data control. This property is predominantly used in a multi-user environment. The following constants are available in order to set the options at run time:
You can select multiple options by adding these constants together. By using the following expression to set the Options property at run time, for example, you can ensure that you have exclusive usage of the records in your Recordset:
dim filepath as string filepath="c:\vb40\" data1.databasename= filepath&"biblio.mdb" data1.option= dbDenywrite + dbreadonly data1.refresh
The ReadOnly property indicates whether the Recordset opened with the Data control can be edited. The following is an example of an expression you could use to set this property at run time:
dim filepath as string filepath="c:\vb40\" data1.databasename= filepath&"biblio.mdb" data1.readonly= True data1.refresh Prevents any changes being made to the Recordset
To practice using the ReadOnly property, perform the following steps.
The RecordsetType property determines that type of Recordset that the Data control creates. The following constants are available to define the Recordset at run time:
In the following example, the code defines a snapshot type of Recordset:
dim filepath as string filepath="c:\vb40\" data1.databasename= filepath&"biblio.mdb" data1.recordsettype= vbRSTypeSnapShot data1.refresh
Each of the Recordset types contains different methods and properties. Subsequent chapters discuss the methods and properties associated with each Recordset type.
The RecordSource property determines the source of the records that make up the Recordset that is held by the Data control. This RecordSource property can contain the name of a table stored in the database, an SQL statement selecting the table and the
fields that are to be part of the Recordset, or the name of a QueryDef that is stored in your database.
Perform the following steps to practice changing the RecordSource:
The Recordset as displayed in the Grid is ordered based on the field that you select for the combo box control (see Figure 27.4). The only exception to this rule would be if you selected the Comments field. In this case, the Recordset order would not
change because it is impossible to order a Recordset by a memo type field.
Figure 27.4. Illustrates the Recordset after it has been sorted by title.
As previously discussed, a Recordset is a collection of records that is held by the Data control. The RecordSource property of the Data control determines the source of the data contained in the Recordset. The RecordsetType property of the Data control
determines the type of the Recordset that the Data control will contain. Through the Recordset property, you are able to manipulate the properties and methods of the Recordset associated with the Data control in the same way that you manipulate a Recordset
object. Chapter 28, "The Access Jet Engine," discusses the various properties and methods that apply to the Recordset object.
Perform the following steps to practice using the Recordset property:
The Data control supports all of the standard events, such as Drag, and mouse movement events that are associated with Visual Basic controls. In addition to the standard events, the Data control supports events that relate to data manipulation. This
section describes the events that relate to data manipulation.
The Error event occurs when there is a data access error that is not related to any of your code and therefore cannot be trapped by standard Visual Basic error handling. An example of this type of error would be if the design time properties that have
been set up in the Data control become invalid at run time, for example, if a field name has been changed but the DataField property in the related text box has not been altered. The Error event consists of two parts. DataErr is the error code
representation of the error that occurred. Response is the response that you want to make in the error condition.
The following constants are available to set the response:
To practice handling an error event, make the following changes to your project:
You will now receive a message box prior to your Form_Loading that indicates that an item was not found in the fields collection.
The Reposition event occurs when a new record becomes the current record. The event can occur as a result of the selection of one of the record navigation buttons on the Data control or when the record is changed in your code through the use of one of
the move or find methods of the Data control or Recordset. The Reposition event has no parameters. The Reposition event differs from the Validate event in that the Validate event occurs before the current record has been changed and the Reposition event
occurs after the new record has been made current. You would use the Reposition event in order to perform an operation each time that the current record in the Recordset has been changed. The Unbound Form example under the earlier Recordset heading in the
chapter illustrated this kind of situation.
The Validate event occurs prior to the current record in the Recordset changing. The following parameters are available in the Validate event:
The following constants are available in order to determine what action triggered the Validate event:
To practice handling a Validate event, make the following changes to your project:
Figure 27.5. Updating a record often causes a dialog box to appear.
The Data control supports all of the standard methods, such as Z-Order, and Drag methods that are associated with Visual Basic controls. In addition to the standard methods, the Data control supports methods that relate to data manipulation. The
following sections discuss these data manipulation methods.
The Refresh method is used in a variety of ways with the Data control. This chapter used the Refresh method in order to effect changes to the underlying Recordset after a property in the Data control had been changed. In addition to the examples
previously illustrated, you must invoke the Refresh method in order to place a newly added record into the correct order in a currently open Recordset. You need to invoke the Refresh method because the default position for the newly added record is at the
end of the Recordset. In addition, the Refresh method is needed in order to allow the newly added record to be visible to other users of a similarly created Recordset. The same would also be true of editing a field that was part of the order by clause in
an SQL statement or changing a field that composed the index of a table. In a multi-user environment, it is important to ensure that an update is always executed after adding, changing, or deleting records so that all users have access to an up-to-date
database.
To practice using the Refresh method, make the following changes to your project:
The UpdateControls method will reset the bound controls to the values they had prior to any editing. This method provides the same effect as re-reading the current record. An example of using the UpdateControls method would be when the user decides that
he does not want to save any changes that have been made.
To practice using the UpdateControls method, make the following changes to your project:
The UpdateRecord method causes the contents of the bound controls to be updated to the database. This method allows you to save any changes and keep the record current. In contrast, previous examples used the Data control to save the record changes only
when the record pointer was changed.
To practice using the UpdateRecord method, make the following changes to your project:
Figure 27.6. The final appearance of the bound control form.
The Data control enables you to access databases other than MS Access databases. To access an external(non-MS Access) database, you must make an entry in the Connect property that matches the type of database you are trying to access. Once you have
connected to the external database, it will behave like a native Access database.
Perform the following steps in order to practice connecting to an external database:
Note that the application behaves in the same manner as when an MS Access format database was used.
So far in this chapter, you have looked at all of the properties, methods, and events that are associated with the Data control. However, there has not been an example presented on how records are removed from a Recordset. The reason for this is that
there is not a delete type of action provided with the Data control. The following example will build upon the final bound control example and add a method in order to delete a record from the underlying Recordset.
Make the following changes to your project:
When you select the Delete button, the currently displayed record is deleted and the Recordset is updated. You could perform the same steps to provide the same functionality to the Bound DBASE form.
This chapter covered the various properties, methods, and events of the Data control. The examples provided have used simple examples in order to illustrate the capabilities of the control. The final version of the form you created allowed you to sort,
display, edit, delete, and add records to the TITLES table of the BIBLIO database as well as that of the external TITLE.DBF file.
In addition to the Data control, you had a very brief look at the Recordset property of the Data control. The Recordset property is very powerful as it allows you to fully exploit the properties and methods of the Jet database engine.
In the following chapter, you will see additional properties and methods that can be applied to Data Access Objects. You can apply the majority of these properties and methods to the Data control through the use of the Recordset property.